package com.dc.juc.example._11_lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description: ReentrantLock   可重入锁_Condition使用
 *
 * @author: gdc
 * @date: 2021/8/16
 * @version 1.0
 */
@Slf4j
public class _2_ReentrantLock {

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();

        // 创建条件
        Condition condition = lock.newCondition();

        new Thread(() -> {
            try {
                lock.lock();
                log.info("wait signal > 1");

                // 使条件处于等待
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            log.info("get signal > 4");
            lock.unlock();
        }).start();

        new Thread(() -> {
            lock.lock();
            log.info("get lock > 2");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            condition.signalAll();
            log.info("send signal > 3");
            lock.unlock();
        }).start();
    }
}
